package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.OrganizationMapper;
import cc.rengu.igas.bsps.common.entity.Organization;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

public class OrganizationMapperImpl implements OrganizationMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public Organization getOrganizationByOrgCode(String orgCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOrganizationByOrgCode");
        Organization organization = new Organization();
        organization.setOrgCode(orgCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, organization, organization);
        if (0 != iReturnCode) {
            rglog.error("根据机构号<{}>获取机构信息失败", orgCode);
            return null;
        }
        return organization;
    }

    @Override
    public Organization selectOrganizationByPrimaryKey(String instId, String orgCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsOrganizationByPrimaryKey");
        Organization organization = new Organization();
        organization.setOrgCode(orgCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, organization, organization);
        if (0 != iReturnCode) {
            rglog.error("根据机构号<{}>获取机构信息失败", orgCode);
            return null;
        }
        return organization;
    }

    @Override
    public List<Organization> getOrganizationByOrgLevel(String instId, String orgLevel) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getBspsOrganizationByOrgLevel");
        Organization organization = new Organization();
        organization.setOrgLevel(orgLevel);
        organization.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, organization, Organization.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据机构名称<{}>获取机构列表失败!", instId, orgLevel);
            return null;
        }
        return objectList.stream().map(item -> (Organization) item).collect(Collectors.toList());
    }

}
